package com.zxc.controller;

import com.zxc.entity.Emp;
import com.zxc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/19 17:36
  * @description:
  */
@Controller
@RequestMapping("/json")
public class Demo4Controller {

    @RequestMapping("/d1")
    @ResponseBody
    public String d1 () {
        //直接写出字符串
        return "ok";
    }

    @RequestMapping("/d2")
    @ResponseBody
    public User d2 () {
        User u=new User();
        u.setName("张三");
        u.setSex("男");
        u.setAge(18);
        return u;
    }

    @RequestMapping("/d3")
    @ResponseBody
    public List<User> d3(){
        User u = new User();
        u.setName("张三");
        u.setSex("男");
        u.setBornDate(new Date());
        User u2 = new User();
        u2.setName("张三2");
        u2.setSex("男2");
        u2.setBornDate(new Date());
        List<User> ul = new ArrayList<>();
        ul.add(u);
        ul.add(u2);
        return ul;
    }


    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/19 19:00
      * @description: @ResponseBody 响应json     @RequestBody 接受json数据
      */
    @RequestMapping("/d4")
    @ResponseBody
    public String d4(@RequestBody Emp u ){
        System.out.println(u);
        return "ok";
    }
}
